package com.czl.module_work.fragment

import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.module_work.BR
import com.czl.module_work.R
import com.czl.module_work.adapter.AppointmentRecordAdapter
import com.czl.module_work.databinding.WorkFragmentAppointmentRecordBinding
import com.czl.module_work.viewModel.AppointmentRecordViewModel
import com.czl.module_work.viewModel.AppointmentRecordViewModel.Companion.FILTER_TYPE_WAITING


/**
 * 创建日期：2022/1/4  16:30
 * 类说明:
 * @author：86152
 */
@Route(path = AppConstants.Router.Work.F_WORK_DEVICE_APPOINTMENT_RECORD)
class AppointmentRecordFragment :
    BaseFragment<WorkFragmentAppointmentRecordBinding, AppointmentRecordViewModel>() {

    private lateinit var mAdapter: AppointmentRecordAdapter

    override fun useBaseLayout(): Boolean = false;

    override fun initContentView(): Int = R.layout.work_fragment_appointment_record

    override fun initVariableId(): Int = BR.viewModel

    override fun initData() {
        viewModel.tvTitle.set("访客记录")
        initAdapter()
        loadData()
    }

    override fun initViewObservable() {
        viewModel.uc.clearOrderEvent.observe(this, {
            mAdapter.setNewInstance(mutableListOf())
        })
        viewModel.uc.loadOrderEvent.observe(this, { data ->
            binding.smartCommon.finishRefresh(true)
            binding.smartCommon.finishLoadMore()
            if(data.isNullOrEmpty())
                return@observe
            mAdapter.addData(data)
        })

        LiveBusCenter.observeAppointmentListRefreshEvent(this, {
            viewModel.refresh()
        })
    }

    private fun loadData() {
        binding.smartCommon.autoRefresh()
    }

    private fun initAdapter() {
        mAdapter = AppointmentRecordAdapter()
        mAdapter.setOnItemClickListener { adapter, view, position ->

            startContainerActivity(AppConstants.Router.Work.F_WORK_APPOINTMENT_DETAIL, Bundle().apply {
                putString(AppConstants.BundleKey.WORK_APPOINTMENT_DETAIL_ID, mAdapter.data[position].applyId?:"")
                putString(AppConstants.BundleKey.WORK_ORDER_STATUS, mAdapter.data[position].guestType?:"1")
            })
        }
        binding.ryCommon.apply {
            adapter = mAdapter
        }
    }
}